package com.china08.yxyapi.service.yxyresource.statistics.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.StringTerms;
import org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder;
import org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.stereotype.Service;

import com.china08.yxyapi.entity.db.res.YxyResPhase;
import com.china08.yxyapi.repository.db.YxyResPhaseRepository;
import com.china08.yxyapi.service.yxyresource.statistics.StatisticsEachSchoolSubjectService;
@Service
public class StatisticsEachSchoolSubjectServiceImpl implements StatisticsEachSchoolSubjectService {
	@Autowired
	private ElasticsearchTemplate et;
	@Autowired
	private YxyResPhaseRepository yxyResPhaseRepository;
	@Override
	public Map<String,Object> statisticsResourceTypeCount(String districtCode) {
		BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
		TermQueryBuilder builderType2 = new TermQueryBuilder("typeId", "yx14");
		TermQueryBuilder builderDel = new TermQueryBuilder("districtCode", districtCode);
		boolQueryBuilder.must(builderType2);
		boolQueryBuilder.must(builderDel);
		SearchRequestBuilder searchRequestBuilder = et.getClient()  
                .prepareSearch("yxy_res").setTypes("resource"); 
		searchRequestBuilder.setSearchType(SearchType.QUERY_THEN_FETCH);  
		TermsBuilder gradeTermsBuilder = AggregationBuilders.terms("phaseIdAgg").field("subjectId");
		searchRequestBuilder.addAggregation(gradeTermsBuilder);
		SearchResponse sr = searchRequestBuilder.setQuery(boolQueryBuilder).execute().actionGet();
		Map<String, Aggregation> aggMap = sr.getAggregations().asMap();
		StringTerms gradeTerms = (StringTerms) aggMap.get("phaseIdAgg");
		Iterator<Bucket> gradeBucketIt = gradeTerms.getBuckets().iterator();
		
		//返回
		Map<String,Object> map=new HashMap<String,Object>();
	    while(gradeBucketIt.hasNext())
	    {
	         Bucket gradeBucket = gradeBucketIt.next();
	         String key = (String) gradeBucket.getKey();
	         long value= gradeBucket.getDocCount();
	         map.put(key, value);
	         
	         System.out.println(key + "科目有" + value +"个资源。");
	         System.out.println();
	    }
		return map;
	}
	@Override
	public Map<String, Object> statisticsResourcePhaseCount(String xueduan,String districtCode) {
		BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
		TermQueryBuilder builderType = new TermQueryBuilder("typeId", "yx14");
		TermQueryBuilder builderDel = new TermQueryBuilder("districtCode", districtCode);
		
		boolQueryBuilder.must(builderType);
		boolQueryBuilder.must(builderDel);
		//1.将学段名称转换成代码
	    YxyResPhase yxyResPhase=yxyResPhaseRepository.findByName(xueduan);
	    //返回
	  	Map<String,Object> map=new HashMap<String,Object>();
	  	if (yxyResPhase!=null&&!yxyResPhase.getId().equals("")) {
	  	    //2.根据学段id查询记录
			TermQueryBuilder builderType2 = new TermQueryBuilder("phaseId", yxyResPhase.getId());
			boolQueryBuilder.must(builderType2);
			SearchRequestBuilder searchRequestBuilder = et.getClient().prepareSearch("yxy_res").setTypes("resource"); 
			searchRequestBuilder.setSearchType(SearchType.QUERY_THEN_FETCH);  
			TermsBuilder gradeTermsBuilder = AggregationBuilders.terms("phaseIdAgg").field("subjectId");
			searchRequestBuilder.addAggregation(gradeTermsBuilder);
			SearchResponse sr = searchRequestBuilder.setQuery(boolQueryBuilder).execute().actionGet();
			Map<String, Aggregation> aggMap = sr.getAggregations().asMap();
			StringTerms gradeTerms = (StringTerms) aggMap.get("phaseIdAgg");
			Iterator<Bucket> gradeBucketIt = gradeTerms.getBuckets().iterator();
		    while(gradeBucketIt.hasNext())
		    {
		         Bucket gradeBucket = gradeBucketIt.next();
		         String key = (String) gradeBucket.getKey();
		         long value= gradeBucket.getDocCount();
		         map.put(key, value);
		         
		         System.out.println(key + "科目有" + value +"个资源。");
		         System.out.println();
		    }
	  	}
		return map;
	}
}
